package com.coin900.coin.cmd;


import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.fu.IFuUserService;
import org.apache.catalina.manager.util.SessionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Unibon
 * <p/>
 * Copyright (c) 2012 YouPeng ValueSoft Inc., All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of
 * ValueSoft Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with ValueSoft.com.
 * <p/>
 * Revision History
 * Date      		Programmer       Notes
 * 2014/12/22   	     stone		     Initial
 * *********************************************************************
 */
public abstract class ACmd implements ICommand, InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        this.scheduler.register(this.getCmdCode() + this.getVersion(), this);
    }

    public String getVersion() {
        return "1";
    }


    public boolean deprecated() {
        return false;
    }

    protected FuUserVO getUser() {
        return null;
    }

    protected FuUserVO getUser(CmdSign sign) {
        Long userId = sign.getUserId();
        if (userId == null) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("id", userId);
        List<FuUserVO> userVOList = this.fuUserService.loadFuUser(params);
        if (userVOList.isEmpty()) {
            return null;
        }
        return userVOList.get(0);
    }


    /**
     * 鉴权
     *
     * @param request
     * @param sign
     * @return
     * @throws Exception
     */
    public CmdSign authCmd(HttpServletRequest request, CmdSign sign) throws Exception {
        return new CmdSign(sign);
    }


    @Autowired
    private Scheduler scheduler;
    @Autowired
    private IFuUserService fuUserService;

}
